<template>
    <div class="content">
        <el-row :gutter="20">
            <el-col :span="12">
                <el-button type="primary" :icon="CirclePlus" @click="onEdit(0,4)">添加专利证书</el-button>
            </el-col>
            <el-col :span="12" class="text-right">
                <el-input class="ml15" v-model="form.patent_owner" style="width: 240px" placeholder="专利权所有人" />
                <el-input class="ml15" v-model="form.search" style="width: 240px" placeholder="请输入关键词" />
                <el-button type="primary" class="ml10" :icon="Search" @click="onSearch">搜索</el-button>
                <el-button type="primary" class="ml10" :icon="RefreshRight" @click="onSearch('Refresh')" />
            </el-col>
        </el-row>
        <el-table class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" align="center" />
            <el-table-column prop="title" label="专利名称" align="center" width="240" />
            <el-table-column prop="aptitude" label="专利类型" align="center" width="100" />
            <el-table-column prop="acquired_at" label="授予时间" align="center" width="120" />
            <el-table-column prop="numbers" label="专利号" width="160" align="center" />
            <el-table-column prop="nationality" label="国别" width="60" align="center" />
            <el-table-column prop="inventor" label="发明人" align="center" show-overflow-tooltip  />
            <el-table-column label="有效期至" width="120" align="center">
                <template #default="scope">
                    <el-text >{{ scope.row.date_type != 1 ? scope.row.end_date : '长期' }}</el-text>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="查看" width="70" align="center">
                <template #default="scope">
                    <el-link type="primary" @click="onShow(scope.row.files)">查看</el-link>
                </template>
            </el-table-column>
            <el-table-column label="完成时间" width="120" align="center">
                <template #default="scope">
                    <el-text v-if="scope.row.files">{{scope.row.updated_at}}</el-text>
                </template>
            </el-table-column>
            <el-table-column prop="patent_owner" label="专利权所有人" width="150" align="center" />
            <el-table-column prop="remarks" label="备注" width="60" align="center" />
            <el-table-column fixed="right" label="操作" width="110" align="center">
                <template #default="scope">
                    <el-link type="primary" @click="onEdit(scope.row.id,form.type)">编辑</el-link>
                    <el-popconfirm name="确定要删除么?" @confirm="onDelete(scope.row.id)">
                        <template #reference>
                            <el-link type="primary" >删除</el-link>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <div class="mt20">
            <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>

    <el-dialog v-model="isCert" title="绑定证书" width="60%" draggable>
        <component :is="certForm" :user_id = userId :type="type" :id = editId @onSave="onSave" />
    </el-dialog>

    <el-dialog v-model="isFile" title="证书查看" width="60%" draggable>
        <component :is="showFile" :data="files"/>
    </el-dialog>

</template>

<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import { CirclePlus, RefreshRight, Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import http from '../../../util/request';
import showFile from '@/components/showFile.vue'
import certForm from "../hr/components/certForm.vue";
const indexMethod = (index: number) => {
  return index+= 1
}
const files = ref('')
const isFile = ref<boolean>(false)
const isCert = ref<boolean>(false)
const userId = ref(0)
const editId = ref(0)
const type = ref(0)
const form = ref<any>({search:'',patent_owner:'',type:4,page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
interface Cert {
    id: number
    title: string
    numbers: string
    type: number
    start_date: string
    end_date: string
    files: string
}
const tableData = ref<Cert[]>([])

const onGetData = () => {
    
    form.value.loading = true
    http.post('/cert',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}


const onSearch = (type = '') => {
    if(type == 'Refresh'){
        form.value.search = ''
        onGetData()
        return
    }
    form.value.page = 0
    onGetData()
}

const onShow = (data: string) => {
    files.value = data
    isFile.value = true
}

const onEdit = (id: number,t:number) => {
    editId.value = id
    type.value = t
    isCert.value = true
}

const onDelete = (id: number) => {
    http.delete('/cert/'+id).then((response:any)=>{
        if(response.data.code != 200){
            ElMessage.error(response.data.msg);return;
        }
        ElMessage.success(response.data.msg);
        onGetData();
    })
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
    onGetData()
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
    onGetData()
}
onMounted(()=>{
    onGetData()
})

const onSave = () => {
    isCert.value = false
    onGetData()
}

</script>

<style scoped>

</style>